<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增DW查询配置')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-param-edit" th:object="${baseDwQryParam}">
        <input name="idDwQryParam" th:field="*{idDwQryParam}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">分类：</label>
            <div class="col-sm-8">
                <select name="sdDwtbClass" class="form-control m-b" th:with="type=${@baseSd.getItem('dw_query_param_class')}">
                    <option th:each="item : ${type}" th:text="${item.na}" th:value="${item.cd}" th:field="*{sdDwtbClass}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">表名：</label>
            <div class="col-sm-8">
                <select name="cdDwTb" class="form-control m-b" th:with="type=${@mdMedTbService.selectMdMedTbBySdTbca('')}" id="cdDwTb" onchange="dwTbChange(this,'')">
                    <option th:each="item : ${type}" th:text="${item.na}" th:value="${item.cd}" th:field="*{cdDwTb}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">字段集合：</label>
            <div class="col-sm-8">
                <select class="form-control " name="fldList" multiple id="fieldSelect">
                    <option value="" th:field="*{fldList}" th:value="*{fldList}"></option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "base/dw/qry";
    $("#form-param-edit").validate({
        focusCleanup: true
    });
    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-param-edit').serialize());
        }
    }
    $(function () {
        dwTbChange($("#cdDwTb"),","+$("#fieldSelect").val()[0]+",");

        $("#fieldSelect").on("select2:select", function (evt) {
            var element = evt.params.data.element;
            var $element = $(element);
            $element.detach();
            $(this).append($element);
            $(this).trigger("change");
        });
    });

    function dwTbChange(ele,fldValue) {
        var cdDwTb = $(ele).val();
        $.ajax({
            type: "POST",
            url: "/md/med/fld/list",
            data: JSON.stringify({"cdTb":cdDwTb}),
            dataType: 'json',
            contentType: 'application/json;charset=UTF-8',
            success: function(res) {
                var data = res.rows;
                var fieldSelectHtml = "";
                $.each(data,function (index,item) {
                    if(fldValue.indexOf(","+item.idPubfld+",")>-1) {
                        fieldSelectHtml += '<option value="'+item.idPubfld+'" selected>'+item.naPubfld+'</option>';
                    } else {
                        fieldSelectHtml += '<option value="'+item.idPubfld+'" >'+item.naPubfld+'</option>';
                    }
                });
                $("#fieldSelect").html(fieldSelectHtml);
            }
        });
    }

    $("input[name='dtSysCre']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='dtSysModi']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });
</script>
</body>
</html>